package com.csun.cmny.provider.web.frontend;

import com.csun.cmny.base.constant.WebSocketConstant;
import com.csun.cmny.provider.model.domain.OpcAppClock;
import com.csun.cmny.provider.model.dto.SendWebSocketRequest;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcAppClockService;
import com.csun.cmny.provider.service.OpcWebSocketService;
import com.csun.cmny.provider.service.impl.JPushService;
import com.csun.cmny.provider.web.websocket.WebSocketServer;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-11 10:27
 * @Description: the class uac api controller
 */
@RestController
@RequestMapping(value = "/api")
@Api(value = "Web-AuthRestController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OpcApiController {

    @Autowired
    private OpcWebSocketService opcWebSocketService;
    @Resource
    private OpcAppClockService opcAppClockService;
    @Resource
    private JPushService jPushService;
    @Resource
    private WebSocketServer webSocketServer;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 推送消息
     */
    @PostMapping(value = "/sendMessageWithUserIdList")
    public Wrapper sendMessageWithUserIdList(@RequestBody SendWebSocketRequest sendWebSocketRequest) {

        sendWebSocketRequest.setMsgType(WebSocketConstant.WEBSOCKET_WITH_USER_ID_LIST);

        opcWebSocketService.sendWebSocketMessage(sendWebSocketRequest);

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    /**
     * 推送消息
     */
    @PostMapping(value = "/sendMessageWithRoleCode")
    public Wrapper sendMessageWithRoleCode(@RequestBody SendWebSocketRequest sendWebSocketRequest) {

        webSocketServer.sendMessageWithRoleCode(sendWebSocketRequest.getRoleCodeList(),
                sendWebSocketRequest.getGroupId(),
                sendWebSocketRequest.getMessage());

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    @PostMapping(value = "/sendClockMsg")
    public Wrapper sendClockMsg(Long id) {

        OpcAppClock c = opcAppClockService.selectByKey(id);

        // 推送闹钟
        Map map = JSONObject.fromObject(c);
        map.put("msgType", "appClock");
        List<String> tagList = Lists.newArrayList("userId_" + c.getUserId());

        String sound = "sound.caf";
        if (c.getBell().equals(1)) {
            sound = "lightM_1.caf";
        }
        jPushService.sendToAll(c.getTitle(), c.getTitle(), c.getTitle(), map.toString(), tagList, sound);

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

}
